﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace library
{
    public partial class borrowInfo : Form
    {
        public borrowInfo()
        {
            InitializeComponent();
        }
        public string adminId = "";
        public string adminName = "";
        private void return_btn_Click(object sender, EventArgs e)
        {
            this.Hide();
            Main frm = new Main();
            frm.Show();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            ClearTexBox();
        }

        protected void ClearTexBox()
        {
            username_box.Text = "";
            bookColId_box.Text = "";
            int year = System.DateTime.Now.Year;
            int month = System.DateTime.Now.Month;
            int day = System.DateTime.Now.Day;
            borDate_box.Value = new DateTime(year, month, day);
           
        }

        private void borrowInfo_Load(object sender, EventArgs e)
        {
            DataBind_borrow();
            int year = System.DateTime.Now.Year;
            int month = System.DateTime.Now.Month;
            int day = System.DateTime.Now.Day;
            borDate_box.Value = new DateTime(year, month, day);
        }

        protected void DataBind_borrow()
        {
            MySqlConnection conn = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
            conn.Open();
            string str = "select * from borrowinfo";
            MySqlDataAdapter da = new MySqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            conn.Close();
            listView1.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem myitem = new ListViewItem(dr["bookColId"].ToString());
                MySqlConnection c = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
                c.Open();
                string sql = string.Format("select name from readerinfo where readerId = {0}", dr["readerId"].ToString());
                MySqlDataAdapter a = new MySqlDataAdapter(sql, c);
                DataTable t = new DataTable();
                a.Fill(t);
                c.Close();
                DataRow r = t.Rows[0];
                myitem.SubItems.Add(r["name"].ToString());
                myitem.SubItems.Add(dr["borDate"].ToString());
                myitem.SubItems.Add(dr["repayDate"].ToString());
                myitem.SubItems.Add(dr["reborNum"].ToString());
                string s = string.Format("select * from admininfo where adminId = {0}", dr["adminId"].ToString());
                DataTable d = SelectSQL(s);
                r = d.Rows[0];
                myitem.SubItems.Add(r["name"].ToString());
                listView1.Items.Add(myitem); //将新建项添加到 ListView 控件中
            }

        }
        
        private DataTable SelectSQL(string sql)
        {
            MySqlConnection conn = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
            conn.Open();
            MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            conn.Close();
            return dt;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string username = username_box.Text.Trim();
            string bookCol = bookColId_box.Text.Trim();
            if(username == "")
            {
                MessageBox.Show("请输入读者姓名");
                return;
            }
            if(bookCol == "")
            {
                MessageBox.Show("请输入馆藏编号");
                return;
            }
            MySqlConnection conn = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
            conn.Open();
            string sql = string.Format("select * from readerinfo where username = '{0}'", username);
            MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            conn.Close();
            DataRow dr = dt.Rows[0];
            string readerId = dr["readerId"].ToString();
            string readerCateId = dr["readerCateId"].ToString();
            string cumBorNum = dr["cumBorNum"].ToString();
            string loss = dr["loss"].ToString();
            if(loss == "1")
            {
                MessageBox.Show("该账户以挂失，不可以借书");
                return;
            }
            string s = string.Format("select status from bookcollectioninfo where bookColId = '{0}'", bookCol);
            DataTable d = SelectSQL(s);
            DataRow r = d.Rows[0];
            string status = r["status"].ToString();
            if(status == "2" || status == "1")
            {
                MessageBox.Show("该书已借出或已丢失，不允许借阅");
                return;
            }
            s = string.Format("select * from readercategory where readerCateId = {0}", readerCateId);
            d = SelectSQL(s);
            r = d.Rows[0];
            string borNum = r["borNum"].ToString();
            if(Convert.ToInt32(borNum) <= Convert.ToInt32(cumBorNum))
            {
                MessageBox.Show("借书数已达上限");
                return;
            }
            //正常借书
            bool falg = borrowBook(bookCol,username,borDate_box.Value.ToString("yyyy-MM-dd"),adminId);
            if (falg)
            {
                MessageBox.Show("借阅成功");
                DataBind_borrow();
                ClearTexBox();
            }
            else
            {
                MessageBox.Show("借阅失败");
            }
        }

        protected bool borrowBook(string ColId, string name, string borDate, string adId)
        {
            MySqlConnection conn = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
            MySqlCommand cmd = new MySqlCommand("borrowBook", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            MySqlParameter[] parames =
            {
                new MySqlParameter("ColId",ColId),
                new MySqlParameter("name",name),
                new MySqlParameter("borDate",borDate),
                new MySqlParameter("adId",adId),
            };
            cmd.Parameters.AddRange(parames);
            conn.Open();
            Object obj = cmd.ExecuteScalar();
            conn.Close();
            if (obj.ToString().Equals("0"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
